package www.com.library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import www.com.common.library.R;


/**
 * 开关切换
 * @author reeta
 *
 */
public class CheckBoxView extends LinearLayout implements View.OnClickListener{

	TintTextView mLeftBtn;
	TintTextView mRightBtn;
	private BtnClickListener mListener;
	private int bg;
	private int itemBg;
	private String leftTitle;
	private String rightTitle;
	private int normalColor;
	private int selectColor;

	public CheckBoxView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		obtainAttributeSet(context, attrs);
		init(context);
	}

	public CheckBoxView(Context context, AttributeSet attrs) {
		super(context, attrs);
		obtainAttributeSet(context, attrs);
		init(context);
	}

	public CheckBoxView(Context context) {
		super(context);
		init(context);
	}

	/**
	 * 一些属性设置
	 * @param c
	 * @param attrs
	 */
	private void obtainAttributeSet(Context c, AttributeSet attrs) {
		TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.CheckBoxView);
		bg = a.getResourceId(R.styleable.CheckBoxView_bg, 0);
		itemBg = a.getResourceId(R.styleable.CheckBoxView_item_bg, 0);
		leftTitle = a.getString(R.styleable.CheckBoxView_left_title);
		rightTitle = a.getString(R.styleable.CheckBoxView_right_title);
		normalColor = a.getColor(R.styleable.CheckBoxView_title_normal_color, 0);
		selectColor = a.getColor(R.styleable.CheckBoxView_title_select_color, 0);
		a.recycle();
	}

	/**
	 * 初始化控件信息
	 * @param ctx
	 */
	private void init(Context ctx) {
		setOrientation(LinearLayout.HORIZONTAL);
		View view = LayoutInflater.from(ctx).inflate(R.layout.commom_check_view, this, true);
		mLeftBtn = (TintTextView) view.findViewById(R.id.btn_left);
		mRightBtn = (TintTextView) view.findViewById(R.id.btn_right);
		mLeftBtn.setOnClickListener(this);
		mRightBtn.setOnClickListener(this);
		mListener = null;
		if(0 != bg){
			view.setBackgroundResource(bg);
		}
		if(0 != itemBg){
			mLeftBtn.setBackgroundResource(itemBg);
			mRightBtn.setBackgroundResource(itemBg);
		}
		if(leftTitle != null && leftTitle.length() > 0){
			mLeftBtn.setText(leftTitle);
		}
		if(rightTitle != null && rightTitle.length() > 0){
			mRightBtn.setText(rightTitle);
		}
		if(normalColor != 0 && selectColor != 0){
			mLeftBtn.setColorValue(normalColor, selectColor);
			mRightBtn.setColorValue(normalColor, selectColor);
		}
		setChecked(false);
	}

	/**
	 * 给右边按钮设置文案
	 * @param titleID
	 */
	public void setRightResource(int titleID) {
		if (titleID == 0) {
			mRightBtn.setVisibility(View.GONE);
		} else {
			mRightBtn.setText(getContext().getString(titleID));
			mRightBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 给左边按钮设置文案
	 * @param titleID
	 */
	public void setLeftResource(int titleID) {
		if (0 == titleID) {
			mLeftBtn.setVisibility(View.GONE);
		} else {
			mLeftBtn.setText(getContext().getString(titleID));
			mLeftBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 给右边按钮设置文案
	 * @param title
	 */
	public void setRightResource(String title) {
		if (null == title || title.equals("")) {
			mRightBtn.setVisibility(View.GONE);
		} else {
			mRightBtn.setText(title);
			mRightBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 给左边按钮设置文案
	 * @param title
	 */
	public void setLeftResource(String title) {
		if (null == title || title.equals("")) {
			mLeftBtn.setVisibility(View.GONE);
		} else {
			mLeftBtn.setText(title);
			mLeftBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 设置左右按钮的点击监听事件
	 * @param l
	 */
	public void setBtnClickListener(BtnClickListener l) {
		mListener = l;
	}

	@Override
	public void setFocusable(boolean focusable) {
		mLeftBtn.setFocusable(focusable);
		mRightBtn.setFocusable(focusable);
		super.setFocusable(focusable);
	}

	/**
	 * 改变左右按钮的状态，主要用于改变按钮的背景
	 * @param isRight
     */
	public void setChecked(boolean isRight) {
		if(isRight){
			mRightBtn.setSelected(true);
			mLeftBtn.setSelected(false);
		}else{
			mLeftBtn.setSelected(true);
			mRightBtn.setSelected(false);
		}
	}

	/**
	 * 返回是否左边被选中
	 * @return
     */
	public boolean isLeftSelect(){
		if(mLeftBtn.isSelected()){
			return true;
		}

		return false;
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.btn_left){
			if (!mLeftBtn.isSelected()) {
				setChecked(false);
				if(mListener != null){
					mListener.onBtnClick(R.id.btn_left);
				}
			}
		}else if(v.getId() == R.id.btn_right){
			if (!mRightBtn.isSelected()) {
				setChecked(true);
				if (mListener != null) {
					mListener.onBtnClick(R.id.btn_right);
				}
			}
		}
	}
}